package com.senjoeson.baselib_ui.base

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.MenuItem
import com.senjoeson.baselib_ui.R

/**
 *
 *@author sunqiao
 *@date 2018/5/28 13:58
 * @function 主要作用是用于头部带有toolbar显示的页面
 */
abstract class BaseToolbarActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(setResId())
        initToolbar(setMainTitle())

        initView()
        initData()
        initListener()
    }


    protected abstract fun initView()
    protected abstract fun initData()
    protected abstract fun initListener()


    /**
     * 设置标题的文字 (选择是否重写)
     */
    open fun setMainTitle(): String {
        return javaClass.simpleName.toString()
    }

    /**
     *toolbar头标题
     */
    private fun initToolbar(mainTitle: String) {
        val mToolbar = findViewById<Toolbar>(R.id.toolbar)
        mToolbar?.setTitle(mainTitle)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mToolbar?.setBackgroundColor(resources.getColor(R.color.colorPrimary,theme))
        }else{
            mToolbar?.setBackgroundColor(resources.getColor(R.color.colorPrimary))
        }

        mToolbar?.setTitleTextColor(Color.WHITE)
        setSupportActionBar(mToolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    /**
     * 设置视图
     */
    abstract fun setResId(): Int


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> onBackArrowEvent()

        }
        return super.onOptionsItemSelected(item)
    }

    /**
     * 左边箭头的点击事件
     */
    abstract fun onBackArrowEvent()

}